import { getDbClient } from "./db.ts";
import * as bcrypt from "https://deno.land/x/bcrypt@v0.4.1/mod.ts";

// 固定的测试密码
const TEST_PASSWORD = "password123";

async function checkAdminPassword() {
  console.log("开始检查管理员密码...");
  
  const client = await getDbClient();
  try {
    console.log("查询数据库中的admin用户...");
    const results = await client.query(
      "SELECT id, username, password_hash FROM users WHERE username = ? AND role = ?",
      ["admin", "admin"]
    );
    
    if (results.length === 0) {
      console.log("错误: 未找到admin用户");
      return;
    }
    
    const user = results[0] as { id: number; username: string; password_hash: string; };
    console.log("用户ID:", user.id);
    console.log("用户名:", user.username);
    console.log("密码哈希:", user.password_hash);
    
    // 测试密码验证
    console.log(`尝试验证密码"${TEST_PASSWORD}"...`);
    const passwordMatch = await bcrypt.compare(TEST_PASSWORD, user.password_hash);
    console.log("密码匹配结果:", passwordMatch);
    
    // 生成新的哈希作为参考
    console.log("生成新的密码哈希作为参考...");
    const newHash = await bcrypt.hash(TEST_PASSWORD);
    console.log("新哈希:", newHash);
    
    // 验证新哈希
    const newHashMatch = await bcrypt.compare(TEST_PASSWORD, newHash);
    console.log("新哈希匹配结果:", newHashMatch);
    
    if (!passwordMatch) {
      console.log("密码验证失败。建议更新数据库中的密码哈希为:", newHash);
    } else {
      console.log("密码验证成功!");
    }
    
  } catch (error) {
    console.error("检查过程中出错:", error);
  } finally {
    await client.close();
  }
}

// 如果直接运行此文件，则执行检查
if (import.meta.main) {
  await checkAdminPassword();
} 